package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommomResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * description: todo
 *
 * @author sun jun
 * @className OrderController
 * @date 2020/3/18 17:15
 */
@RestController
@Slf4j
public class OrderController {

//    public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommomResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommomResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommomResult<Payment> getPayment(@PathVariable("id") Long id){
        System.out.println("id"+id);
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommomResult.class);

    }

    @GetMapping("/consumer/payment/getEntity/{id}")
    public CommomResult<Payment> getPaymentForEntity(@PathVariable("id") Long id){
        System.out.println("id"+id);
        ResponseEntity<CommomResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommomResult.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return new CommomResult<>(0, "操作失败");
        }


    }
}
